<template>
    <div>
        <el-table :data="userlist" border style="width: 100% ;" class="table">
            <el-table-column prop="uid" label="用户编号">
            </el-table-column>

            <el-table-column prop="username" label="用户名称">
            </el-table-column>

            <el-table-column prop="rolename" label="所属角色">
            </el-table-column>

            <el-table-column label="状态">
                <template slot-scope="item">
                    <div>
                        <el-tag type="success" plain size="medium" v-if="item.row.status == 1">启用</el-tag>
                        <el-tag type="danger" plain size="medium" v-else>禁用</el-tag>
                    </div>
                </template>
            </el-table-column>

            <el-table-column label="操作">
                <template slot-scope="item">
                    <div>
                        <el-button type="primary" icon="el-icon-edit" size="mini" @click="edit(item.row.uid)">编辑</el-button>
                        <el-button type="danger" icon="el-icon-delete" size="mini" @click="del(item.row.uid)">删除</el-button>
                    </div>
                </template>
            </el-table-column>

        </el-table>
        <!-- 
            page-size	每页显示条目个数，支持 .sync 修饰符

         -->
        <el-pagination background layout="prev, pager, next" :total="count" :page-size="size" class="fen" @current-change="change"></el-pagination>
    </div>
</template>
<script>
import { userdelete } from "../../request/api"
import { mapGetters, mapActions } from "vuex"
export default {
    data() {
        return {
            

        }
    },
    methods: {
        ...mapActions({
            getUserList: "user/getUserListActions",
            getUserCount:"user/getUserCount"

        }),
        //编辑
        edit(uid) {
            this.$emit("edit", uid)
        },
        // 删除
        del(uid) {
            // console.log(uid);
            // 用户点击删除的时候跳出提示框 是elementUI提供的
            this.$confirm('是否永久删除?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                // 调用删除接口,把要删除的数据的ID传过去
                userdelete({uid})
                .then((res)=>{
                    // console.log(res);
                    if(res.code==200){
                        this.$message(res.msg);
                        //重新请求总条数
                        this.$store.dispatch("user/getUserCount")
                    }
                })
                this.$message({
                    type: 'success',
                    message: '删除成功!'
                });
                // 重新调用vuex库中的方法 每次删除成功之后请求一次数据实时更新
                this.$store.dispatch("user/getUserListActions")
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            })
        },
        change(e){
            // console.log(e);
            //点击切换的时候重新请求
            this.$store.dispatch("user/changePageActions",e)
        }

    },
    mounted() {
        this.getUserList()
        this.getUserCount()
       
    },
    watch: {},
    computed: {
        ...mapGetters({
            userlist: "user/getUser",
            size:"user/getSize",
            count:"user/getCount",

        })
    },
    filters: {},
    components: {

    }
}
</script>
<style scoped>
.fen {
    position: absolute;
    right: 10px;
    margin-top: 10px;

}
</style>